package com.ruoyi.project.lesson.lesson.service.impl;

import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.lesson.block.domain.LessonBlock;
import com.ruoyi.project.lesson.block.mapper.LessonBlockMapper;
import com.ruoyi.project.lesson.lesson.domain.Lesson;
import com.ruoyi.project.lesson.lesson.mapper.LessonMapper;
import com.ruoyi.project.lesson.lesson.service.ILessonService;
import com.ruoyi.project.lesson.tag.domain.LessonTag;
import com.ruoyi.project.lesson.tag.mapper.LessonTagMapper;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-12-07
 */
@Service
@DataSource(DataSourceType.KKFORMULA)
public class LessonServiceImpl implements ILessonService {

    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private LessonBlockMapper lessonBlockMapper;

    @Autowired
    private LessonTagMapper lessonTagMapper;

    /**
     * 查询课程
     *
     * @param id 课程ID
     * @return 课程
     */
    @Override
    public Lesson selectLessonById(Long id) {
        return lessonMapper.selectLessonById(id);
    }

    /**
     * 查询课程列表
     *
     * @param query 课程
     * @return 课程
     */
    @Override
    public List<Lesson> selectLessonList(Lesson query) {
        List<Lesson> lessons = lessonMapper.selectLessonList(query);
        for (Lesson lesson : lessons) {
            List<Long> blockIds = JsonUtil.toListObject(lesson.getBlocks(), Long.class);
            List<String> blockNames = new ArrayList<>();
            if (!CollectionUtils.isEmpty(blockIds)) {
                for (Long blockId : blockIds) {
                    LessonBlock block = lessonBlockMapper.selectLessonBlockById(blockId);
                    if (block != null) {
                        blockNames.add(block.getName());
                    }
                }
            }
            lesson.setBlockNames(blockNames);

            List<Long> tagIds = JsonUtil.toListObject(lesson.getTags(), Long.class);
            List<String> tagNames = new ArrayList<>();
            if (!CollectionUtils.isEmpty(tagIds)) {
                for (Long tagId : tagIds) {
                    LessonTag tag = lessonTagMapper.selectLessonTagById(tagId);
                    if (tag != null) {
                        tagNames.add(tag.getName());
                    }
                }
            }
            lesson.setTagNames(tagNames);
        }
        return lessons;
    }

    /**
     * 新增课程
     *
     * @param lesson 课程
     * @return 结果
     */
    @Override
    public int insertLesson(Lesson lesson) {
        lesson.setCreateBy(ShiroUtils.getLoginName());
        lesson.setProvideMethods(JsonUtil.toJson(lesson.getProvideMethodIds()));
        return lessonMapper.insertLesson(lesson);
    }

    /**
     * 修改课程
     *
     * @param lesson 课程
     * @return 结果
     */
    @Override
    public int updateLesson(Lesson lesson) {
        lesson.setProvideMethods(JsonUtil.toJson(lesson.getProvideMethodIds()));
        return lessonMapper.updateLesson(lesson);
    }

    /**
     * 删除课程对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLessonByIds(String ids) {
        return lessonMapper.deleteLessonByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程信息
     *
     * @param id 课程ID
     * @return 结果
     */
    @Override
    public int deleteLessonById(Long id) {
        return lessonMapper.deleteLessonById(id);
    }
}
